<?
//////////////////////////////////////////////////////////////////////////////////////
//                                                                                  //
//                                   MiniGal                                        //
//                                                                                  //
//                      http://www.thomasrybak.com/minigal                          //
//                           minigal@thomasrybak.com                                //
//                                                                                  //
//    MiniGal is free software; you can redistribute it and/or modify               //
//    it under the terms of the GNU General Public License as published by          //
//    the Free Software Foundation; either version 2 of the License, or             //
//    (at your option) any later version.                                           //
//                                                                                  //
//    MiniGal is distributed in the hope that it will be useful,                    //
//    but WITHOUT ANY WARRANTY; without even the implied warranty of                //
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 //
//    GNU General Public License for more details.                                  //
//                                                                                  //
//    You should have received a copy of the GNU General Public License             //
//    along with this program; if not, you can find it here:                        //
//    http://www.gnu.org/copyleft/gpl.html                                          //
//                                                                                  //
//////////////////////////////////////////////////////////////////////////////////////

session_start();
// IS MINIGAL CONFIGURED?
if (isset($_REQUEST['debug']) && ($_REQUEST['debug'] == "yes")) { error_reporting(E_ALL); }
else { error_reporting(0); } //Turn error reporting on when debugging, else off
if (!is_file("settings.php")) {
  include("admin.php");
  exit();
}

//////////////////////////////
//                          //
//   INCLUDES & VARIABLES   //
//                          //
//////////////////////////////

if (isset($_REQUEST['list'])) {
  if (get_magic_quotes_gpc()) { $list = stripslashes($_REQUEST['list']); }
  else { $list = $_REQUEST['list']; }
} else { $list = ''; }
if (isset($_REQUEST['image'])) {
  if (get_magic_quotes_gpc()) { $image = stripslashes($_REQUEST['image']); }
  else $image = $_REQUEST['image'];
}

$page  = (isset($_REQUEST["page"])) ? $_REQUEST["page"] : '';

include("settings.php");
include("classes.php");
if (is_file("counter.php")) { include('counter.php'); }

$MGaction  = new Minigal;
include("language.php"); // Load language file
$MGaction->activestyle = $activestyle;
$MGaction->gallerydir = $gallerydir;
$MGaction->list = $list;
$MGaction->image = $image;
$MGaction->page = $page;
$MGaction->dirlist();
$MGaction->images_side = $images_side;
$MGaction->pagerows = $pagerows;
$MGaction->gallerytitle = $gallerytitle;
$MGaction->dateformat = $dateformat;
$MGaction->owneremail = $owneremail;
$MGaction->gallerycopyright = $gallerycopyright;
$MGaction->upload_height = $upload_height;
$MGaction->upload_width = $upload_width;
$MGaction->sendmail = $sendmail;

$movie_ext= array(".mov",".avi",".mpg",".mpeg",".wmv",".asf");

if (isset($_GET['activestyle'])) { $activestyle = $_GET['activestyle']; };

$screenwidth = 999999999;
if ($resizelargeimg != "0") { $screenwidth = $resizelargeimg; }


//////////////////////////////
//                          //
//        DEBUG MODE        //
//                          //
//////////////////////////////

include("styles/$activestyle/templates/template_header.php");
$MGaction->debug();

//////////////////////////////
//                          //
//       MAIN GALLERY       //
//                          //
//////////////////////////////
if (!isset($image)) {

  $MGaction->folderpwdcheck();
  $MGaction->showfolderintro();
  if (@count($MGaction->dirarray) > 0) { ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
 <tr valign="top">
<?  
    for ($i = 0; $i < count($MGaction->dirarray); $i++) {
      $thumb = $MGaction->setthumb($MGaction->dirarray[$i]);
      list($width, $height, $type, $attr) = getimagesize("$thumb");
      $link = "index.php?list=" . stripslashes(ltrim($list."/","/") . $MGaction->dirarray[$i]);
?>
  <td>
<? include("styles/$activestyle/templates/template_folders.php"); ?> 
  </td>
<?    if (is_int(($i+1) / $images_side) && ($i+1 < count($MGaction->dirarray))) {  ?>
 </tr><tr valign="top">
<?
      }  
    }
?>
</tr></table> 
<p>&nbsp;</p>
<?
    if (count($MGaction->filearray) > 0) { echo "<hr /><br />"; }
  }

// Echo Files

  $MGaction->pagecounter();
  if (count($MGaction->filearray) > 0) {
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr valign="top">
<?
    $from = ($MGaction->page - 1) * $images_side * $pagerows;
    $to = (($MGaction->page * $images_side * $pagerows) < count($MGaction->filearray)) ? 
           ($MGaction->page * $images_side * $pagerows) : count($MGaction->filearray);
    if ($page == "all") { 
      $from = 0; 
      $to = count($MGaction->filearray); 
    }
    if (strlen($list) > 0) { $list .= "/"; } //Set list variable ending slash
    for ($i = $from; $i < $to; $i++) {
        if (in_array(strrchr($MGaction->filearray[$i], "."),$movie_ext)) { //Check if movie=true
          $link = $list . $MGaction->filearray[$i];
          $file = "styles/$activestyle/images/movie.gif";
          $target = "_blank";
          $width = 150;
          $height = 100;
          $alt = str_replace(strrchr($MGaction->filearray[$i],"."),"",$MGaction->filearray[$i]);
          $title = $MGaction->lang['movie'];
          $comment_icon = "img_bottomright";
        } else {
          list($width, $height, $type, $attr) = getimagesize($list."thumbs/tn_".$MGaction->filearray[$i]);
          $link = "index.php?image=" . $MGaction->filearray[$i] . "&amp;list=" . rtrim($list,"/") . "&amp;page=" . $MGaction->page;
          $file = $list. "thumbs/tn_" . $MGaction->filearray[$i];
          $target = "_self";
          $alt = "";
          $comment_icon = (is_file(rtrim($list,"/") . "/thumbs/" . $MGaction->filearray[$i] . ".txt")) ? "img_comment" : "img_bottomright";
        }
?>
    <td height="<? echo ($upload_height+75); ?>">
<?
        include("styles/$activestyle/templates/template_files.php");
?>
    </td>
<?
        if (is_int(($i + 1) / $images_side) &&  $i < ($to - 1)) {
?>
  </tr>
  <tr valign="top">
<?
        }
    }
?>
  </tr>
</table>
<?
  }

  if (strlen($list)>0) { rtrim($list, "/"); } //Un-Set list variable ending slash

  // IF FOLDER IS EMPTY:

  if (@count($MGaction->dirarray) < 1 && count($MGaction->filearray) < 1) {
    echo "<div align=\center\><br />" .  $MGaction->lang['empty folder'] . "</div>\n";
  }
  $MGaction->pagecounter();

} else {

//////////////////////////////
//                          //
//    SHOW SINGLE IMAGE     //
//                          //
//////////////////////////////

  if (strlen($list) > 0) { $list .= "/"; }
  $MGaction->folderpwdcheck();

  $screenwidth = $screenwidth - 100;
  // IF MOVIE
  if (in_array(strrchr("$gallerydir/$list$image", "."), $movie_ext)) {
    $width = 150;
    $height = 100;
    $link = "$gallerydir/$list$image";
    $image = "styles/$activestyle/images/movie.gif";
    $target = "_blank";
    $title = $MGaction->lang['movie'];
  } else  { 
    // IMAGE DON'T RESIZE IT
    list($width, $height, $type, $attributes) = getimagesize("$gallerydir/$list$image");
    if ($width <= $screenwidth) {
      $link = "index.php?list=" . rtrim($list,"/") . "&amp;page=" . $page;
      $image = $list . $image;
      $target = "_self";
      $title = "";
      $topright = "";
    } else  { 
      // IMAGE RESIZE IT
      list($width, $height, $type, $attributes) = getimagesize("$gallerydir/" . $list . "thumbs/tn_med_$image");
      $link = "index.php?list=" . rtrim($list,"/") . "&amp;page=" . $page;
      $largeimage = $list . $image;
      $image = $list . "thumbs/tn_med_" . $image;
      $target = "_self";
      $title = "";
      $topright = "<a href=\"$largeimage\" target=\"_blank\"><img src=\"styles/$activestyle/images/dir_topright_resized.gif\" border=\"0\" width=\"26\" height=\"26\" alt=\"".$MGaction->lang['downsized']."\" /></a>";
    }
  }
  include("styles/$activestyle/templates/template_showimage.php");
  if (strlen($list) > 0) { rtrim($list, "/"); }
}

//////////////////////////////////////////////////////////////
//                                                          //
//   DON'T REMOVE THIS LINK: YOU WILL VOID THE LICENSE!!!   //
//                                                          //
//////////////////////////////////////////////////////////////
if ($list=="" && $image=="") {
?>
<div class="credits">Powered by <a href="http://www.thomasrybak.com/minigal" target="_blank">MiniGal</a> (<? echo $version ?>)</div>
<br />
<?
}

include("styles/$activestyle/templates/template_footer.php");
?>
